package www.houdoudou.com.supplier;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.houdoudou.com.owner.OwnerManagement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public class SupplierManagement {
	
	public static	JSONObject  managerSupplier(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("FunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		   switch(FunctionCode){
		   
		// 查询 
			case 1:{
				
					
					// step2 查询字管理员
					  
				 String Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
				   try {
					   String sql = "select * from Tb_Supplier where Supplier_Telnum="+Supplier_Telnum+"";    
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					if(rs.next()){	

									result.put("ActionStatus", true);
									result.put("ErrorInfo", "查询成功");						
									result.put("Supplier_Name", rs.getString("Supplier_Name"));
									result.put("Supplier_Address", rs.getString("Supplier_Address"));
									result.put("Minute_NoOrder", rs.getInt("Minute_NoOrder"));
									double temp=(double)rs.getDouble("Balance_Rate");
									result.put("Balance_Rate", temp);
									result.put("Supplier_isAuthority", rs.getString("Supplier_isAuthority"));							
									result.put("Balance_Type", rs.getString("Balance_Type"));
									result.put("Update_No", rs.getInt("Update_No"));
									result.put("Sale_Total", rs.getInt("Sale_Total"));
									result.put("Product_Total_Num", rs.getInt("Product_Total_Num"));								
									result.put("Major_Business", rs.getString("Major_Business"));
									result.put("Service_Telnum", rs.getString("Service_Telnum"));
									
								
							}else{
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "要查询的用户不存在");
							}
							
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "数据库查询异常");
						}    			
					   break;     } //case 1
			//修改 按身份进行了区别。
			case 2:{
				 String Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
				   try {
					   String sql = "select * from Tb_Supplier where Supplier_Telnum="+Supplier_Telnum+"";    
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
						rs= stm.executeQuery(sql);
					if(rs.next()){		
							 Manager_Authority=jsonfrom.getInt("Manager_Authority");
							switch(Manager_Authority){
						
							case 4:{
								rs.updateString("Supplier_Name", jsonfrom.getString("Supplier_Name"));
								rs.updateString("Supplier_Address", jsonfrom.getString("Supplier_Address"));
								rs.updateString("Balance_Type", jsonfrom.getString("Balance_Type"));
								rs.updateString("Supplier_isAuthority", jsonfrom.getString("Supplier_isAuthority"));
								rs.updateDouble("Balance_Rate", jsonfrom.getDouble("Balance_Rate"));
								rs.updateInt("Minute_NoOrder", jsonfrom.getInt("Minute_NoOrder"));
								rs.updateInt("Update_No", rs.getInt("Update_No")+1);
								rs.updateRow();
								result.put("ActionStatus", true);
								result.put("ErrorInfo", "更新成功");
							
							                break;}
							
							
							// 代表供应商自己，可以修改自己的密码
							case 5:{
								rs.updateString("Supplier_Secret", jsonfrom.getString("New_Supplier_Secret"));
								rs.updateRow();
								result.put("ActionStatus", true);
								result.put("ErrorInfo", "更新成功");
							
							                break;}
							
							}																																						
					
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "要修改的用户不存在");
						}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "数据库查询异常");
				}
				
			break;} //case 2
			
			// 新增 
			case 4:{
				 if(Manager_Authority==4){
						// step2 检查是否已经存在
						 String Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
						   try {
							   String sql = "select * from Tb_Supplier where Supplier_Telnum="+Supplier_Telnum+"";    
								stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
								rs= stm.executeQuery(sql);
							if(!rs.next()){	
						
			
						
						sql = "insert  into Tb_Supplier (Supplier_Telnum,Supplier_Secret,Supplier_Name,Supplier_Address,Owner_Telnum,Balance_Rate,Balance_Type,Supplier_isAuthority,Minute_NoOrder,Update_No) values (?,?,?,?,?,?,?,?,?,?)";
			              	pstm=conn.prepareStatement(sql);
							pstm.setString(1, jsonfrom.getString("Supplier_Telnum"));
							pstm.setString(2, jsonfrom.getString("Supplier_Secret"));
							pstm.setString(3, jsonfrom.getString("Supplier_Name"));
							pstm.setString(4, jsonfrom.getString("Supplier_Address"));
							pstm.setString(5, jsonfrom.getString("Owner_Telnum"));
							
		
							pstm.setDouble(6, jsonfrom.getDouble("Balance_Rate"));
							pstm.setString(7, jsonfrom.getString("Balance_Type"));
							pstm.setString(8, jsonfrom.getString("Supplier_isAuthority"));
							pstm.setInt(9, jsonfrom.getInt("Minute_NoOrder"));
							pstm.setInt(10, jsonfrom.getInt("Update_No"));
							
						    pstm.executeUpdate();
						    result.put("ActionStatus", true);
							result.put("ErrorInfo", "增加成功");
							}else{
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "添加失败，用户已经存在");
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "数据库异常");
						}
						 }// end of if manager_authority==3
						 else{
							 result.put("ActionStatus", false);
								result.put("ErrorInfo", "你没有权限操作");}			
			   break;}// case 4
			//查询所有
			 case 5:{
				  try {
					 		 
					   String sql = "select * from  Tb_Supplier";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
						JSONArray Suppliers= new JSONArray();
						while(rs.next()){
							JSONObject supplier= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){
						
						case 1:{	isadd=true;
						              break;}
						case 2:{	isadd=true;
			                          break;}
						case 3:{ // 这里权限还有问题，
							       isadd=true;
							        break;}	
						case 4:{
							if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
								isadd=true;
							        break;}	
			              }
						if(isadd){
							supplier.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							supplier.put("Supplier_Name", rs.getString("Supplier_Name"));
							supplier.put("Supplier_isAuthority", rs.getString("Supplier_isAuthority"));
	                        Suppliers.add(supplier);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Suppliers",Suppliers);
				   
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "查询失败");
					} 
						
						
					 break;  } //case5
			
			
			
		   }// end switch functioncode
		   
		   CommonFunction.closeConn(conn, pstm, stm, rs);
		   
		   return result;
	}
  
	public static  JSONObject  loginCheck(String jsonData){
		 // step1 所有数据定义
		   JSONObject result= new JSONObject();
	
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		 // step2 数据解析 	  
		   JSONObject json= JSONObject.fromObject(jsonData);
		   String Supplier_Telnum=json.getString("Supplier_Telnum");
		   String Supplier_Secret=json.getString("Supplier_Secret");
		 
		   try {
		   String sql = "select * from Tb_Supplier where Supplier_Telnum="+Supplier_Telnum+"";  
			stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
			rs= stm.executeQuery(sql);
			if(rs.next()){
				String correctSecret= rs.getString("Supplier_Secret");
				if(correctSecret.equals(Supplier_Secret)){
				String Supplier_isAuthority=rs.getString("Supplier_isAuthority"); 
					if(Supplier_isAuthority.equals("1")){
					
					//检验业主是否授权
					String Owner_Telnum=rs.getString("Owner_Telnum");
				JSONObject ownerCheckJson=OwnerManagement.authorityCheck(Owner_Telnum, conn);
				    if(ownerCheckJson.getBoolean("ActionStatus")){
				    	result.put("ActionStatus", true);
						result.put("ErrorInfo", "登陆成功");
						result.put("Owner_Telnum",Owner_Telnum);
				    }else{
				    	result=ownerCheckJson;
				    }
					
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "该用户没有被授权");
					}
					
				
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "密码不正确");
				}
			}else{
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "不存在该用户");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		   
		   CommonFunction.closeConn(conn, pstm, stm, rs);
		   return result;
	 }
	
	
}
